#include<bits/stdc++.h>
#define sd(n) scanf("%d",&n) 
#define sld(n) scanf("%lld",&n)
#define pd(n) printf("%d", (n))
#define pld(n) printf("%lld", n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define fi first
#define se second
const int N = 2e5;
#define INF 0x7fffffff
typedef long long int ll;
using namespace std;
//----------------------------------------------------------------------------//
int ans = 0;
int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
string s1, s2;
char a[4][4];
char b[4][4];
char st[4][4];
unordered_set<string> judge;
struct node
{
	//char a[4][4];
	string _str;
	int dist;
	int x, y;
};
bool bfs(int sx, int sy)
{
	queue<node> q;
	q.push({s1,0,sx,sy});
	while (!q.empty())
	{
		auto t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = dx[i] + t.x;
			int y = dy[i] + t.y;
			int dist_cur = t.dist + 1;
			string cur_str=t._str;
			if (x < 0 || x >= 3 || y < 0 || y >= 3) continue;


			int pos = 0;
			char cur[4][4];
			for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++)
					cur[i][j] = cur_str[pos++];

			swap(cur[x][y], cur[t.x][t.y]);


			bool ok = true;
			for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++)
				{
					if (cur[i][j] != b[i][j]) ok = false;
					cur_str += cur[i][j];
				}
			//ed:;
			if (ok)
			{
				ans = dist_cur; return true;
			}
			if (judge.find(cur_str) == judge.end())
			{
				judge.insert(cur_str);
				q.push({cur_str, dist_cur, x, y });
			}

		}
	}
	return false;
}



int main()
{
	cin >> s1 >> s2;
	int pos = 0, x, y;

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			a[i][j] = s1[pos];
			b[i][j] = s2[pos++];
			if (a[i][j] == '.') x = i, y = j;
		}
	}
	judge.insert(s1);
	if (bfs(x, y)) pd(ans);
	else pd(-1);
	puts("");

	return 0;
}